#!/bin/bash

## 一键配置ubuntu环境及预装常用软件(Ubuntu18.04下测试)(暂时还不兼容WSL版本)20211218

###############################################
#Linux登录问候语

# echo '
# ██╗    ██╗ █████╗ ███╗   ██╗ ██████╗ 
# ██║    ██║██╔══██╗████╗  ██║██╔════╝ 
# ██║ █╗ ██║███████║██╔██╗ ██║██║  ███╗
# ██║███╗██║██╔══██║██║╚██╗██║██║   ██║
# ╚███╔███╔╝██║  ██║██║ ╚████║╚██████╔╝
#  ╚══╝╚══╝ ╚═╝  ╚═╝╚═╝  ╚═══╝ ╚═════╝ 
                                    
# Hello,Linux!
# ' > /etc/motd

echo '
██╗     ██╗███╗   ██╗██╗   ██╗██╗  ██╗
██║     ██║████╗  ██║██║   ██║╚██╗██╔╝
██║     ██║██╔██╗ ██║██║   ██║ ╚███╔╝ 
██║     ██║██║╚██╗██║██║   ██║ ██╔██╗ 
███████╗██║██║ ╚████║╚██████╔╝██╔╝ ██╗
╚══════╝╚═╝╚═╝  ╚═══╝ ╚═════╝ ╚═╝  ╚═╝
                                      
Hello,Linux!
' > /etc/motd

# echo '
# ██╗  ██╗███████╗██╗     ██╗      ██████╗ 
# ██║  ██║██╔════╝██║     ██║     ██╔═══██╗
# ███████║█████╗  ██║     ██║     ██║   ██║
# ██╔══██║██╔══╝  ██║     ██║     ██║   ██║
# ██║  ██║███████╗███████╗███████╗╚██████╔╝
# ╚═╝  ╚═╝╚══════╝╚══════╝╚══════╝ ╚═════╝ 
                                         
# Hello,Linux!
# ' > /etc/motd

###############################################
#设置时区
mv /etc/localtime /etc/localtime.bak
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

echo "`date +'%Y-%m-%d %H:%M:%S'`: start !" >> ~/.log
###############################################

# #同步时间
# ntpdate cn.pool.ntp.org
# hwclock -w

#apt换国内源
cp /etc/apt/sources.list /etc/apt/sources.bak1

echo 'deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
 
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
 
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
 
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
 
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse' > /etc/apt/sources.list

apt update
apt upgrade -y

echo "`date +'%Y-%m-%d %H:%M:%S'`: apt update Done !" >> ~/.log
###############################################

#安装常用环境
apt install -y build-essential checkinstall
apt install -y gcc g++ make
apt install -y libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev

#安装常用软件
apt install -y openssh-server vim net-tools screen tree wget curl git vifm htop nmap tasksel supervisor psmisc

echo "`date +'%Y-%m-%d %H:%M:%S'`: apt install Done !" >> ~/.log
###############################################

#安装python3及相关环境包
apt install -y python3 python3-pip python3-setuptools
apt install software-properties-common

#pip3换国内源并升级到最新版本
mkdir ~/.pip
echo '[global]
trusted-host = mirrors.aliyun.com
index-url = http://mirrors.aliyun.com/pypi/simple' > ~/.pip/pip.conf

pip3 install --upgrade pip

#pip3安装常用的python库
pip3 install requests
pip3 install flask
pip3 install flask-restful
pip3 install pymysql
pip3 install scp
pip3 install tcping
pip3 install paramiko
pip3 install pycrypto
pip3 install pexpect
pip3 install fabric

echo "`date +'%Y-%m-%d %H:%M:%S'`: python3 Done !" >> ~/.log
###############################################

#安装指定服务软件

# 安装并配置sshd
apt install -y openssh-server openssh-client

#创建sshd配置文件
echo 'PermitRootLogin yes
PasswordAuthentication yes
PermitEmptyPasswords no
ChallengeResponseAuthentication no
GSSAPIAuthentication no
UseDNS no
ClientAliveInterval 60
ClientAliveCountMax 7200
Subsystem sftp  /usr/lib/openssh/sftp-server
GatewayPorts yes
AllowTcpForwarding yes' > /etc/ssh/sshd_config

systemctl restart ssh

echo "`date +'%Y-%m-%d %H:%M:%S'`: sshd Done !" >> ~/.log
###############################################

# 安装并配置docker
apt install -y docker.io python-pip docker-compose
systemctl enable docker

#docker换国内镜像源
echo '{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}' > /etc/docker/daemon.json

systemctl daemon-reload
systemctl restart docker

echo "`date +'%Y-%m-%d %H:%M:%S'`: docker Done !" >> ~/.log
###############################################

# 安装并配置mysql5.7
apt install -y mysql-server mysql-client libmysqlclient-dev

#创建mysql配置文件
echo '[mysqld_safe]
socket		= /var/run/mysqld/mysqld.sock
nice		= 0

[mysqld]
#
# * Basic Settings
#
user		= mysql
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
port		= 3306
basedir		= /usr
datadir		= /var/lib/mysql
tmpdir		= /tmp
lc-messages-dir	= /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address		= 127.0.0.1
#
# * Fine Tuning
#
key_buffer_size		= 16M
max_allowed_packet	= 16M
thread_stack		= 192K
thread_cache_size       = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover-options  = BACKUP
#max_connections        = 100
#table_open_cache       = 64
#thread_concurrency     = 10
#
# * Query Cache Configuration
#
query_cache_limit	= 1M
query_cache_size        = 16M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
#slow_query_log		= 1
#slow_query_log_file	= /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id		= 1
#log_bin			= /var/log/mysql/mysql-bin.log
expire_logs_days	= 10
max_binlog_size   = 100M
#binlog_do_db		= include_database_name
#binlog_ignore_db	= include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem' > /etc/mysql/mysql.conf.d/mysqld.cnf

#创建两个mysql账户
mysql -e "grant all on *.* to root@'%' identified by 'rootmysqlpassword' with grant option"
mysql -e "grant all on *.* to test@'%' identified by 'testmysqlpassword' with grant option"

systemctl restart mysql

echo "`date +'%Y-%m-%d %H:%M:%S'`: mysql Done !" >> ~/.log
###############################################

# 安装并配置nginx
apt install -y nginx

#修改nginx配置文件

echo "`date +'%Y-%m-%d %H:%M:%S'`: nginx Done !" >> ~/.log
###############################################

# 安装并配置git
apt install -y git-all
# git config --global user.name "xxxxxx"
# git config --global user.email "xxxxxx@xx.com"

echo "`date +'%Y-%m-%d %H:%M:%S'`: git Done !" >> ~/.log
###############################################


#无人值守lanp(含配置MySQL账户密码,远程访问等)


###############################################

#ufw防火墙安装及配置
# apt install ufw
# ufw enable
# ufw default deny
# ufw allow 22
# ufw allow ssh
# ufw allow 443
# ufw allow https
# ufw allow 80


###############################################












###############################################
#执行完毕,记录并重启
echo "`date +'%Y-%m-%d %H:%M:%S'`: End !" >> ~/.log
echo "`date +'%Y-%m-%d %H:%M:%S'`: reboot !" >> ~/.log
reboot
###############################################